// pages/record.js
const app = getApp();
const db = wx.cloud.database();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        inputBeizhu : "",
        inputPrice : "",
        index:0,
        year:0,
        month:0,
        day:0,
        array:["支出","收入"],
        yearArray:[2022,2021,2020,2019],
        monthArray:[1,2,3,4,5,6,7,8,9,10,11,12],
        dayArray:[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31]
    },

    dataInput:function(e){
        this.data.inputData=e.detail.value;
    },
    beizhuInput:function(e){
        this.data.inputBeizhu=e.detail.value;
    },
    priceInput:function(e){
        this.data.inputPrice=e.detail.value;
    },

    post:function(options){
        if(this.data.inputPrice!=''){
            var recordObject = new Object();
            recordObject.inputData = this.data.yearArray[this.data.year].toString()+
            '.'+this.data.monthArray[this.data.month].toString()+'.'+this.data.dayArray[this.data.day].toString();
            recordObject.inputBeizhu = this.data.inputBeizhu;
            recordObject.inputPrice = this.data.inputPrice;
            recordObject.inputIndex = (this.data.index==0?"支出":"收入");
            app.globalData.dataArray.push(recordObject);
            app.globalData.maxNum += 1;
            var temp  = parseInt(this.data.inputPrice);
            if(this.data.index==0){
                if(app.globalData.currentData==recordObject.inputData){
                    app.globalData.day_out += temp;
                    app.globalData.all_out += temp;
                }
                else{
                    app.globalData.all_out += temp;
                }
            }
            else{
                if(app.globalData.currentData==recordObject.inputData){
                    app.globalData.day_in += temp;
                    app.globalData.all_in += temp;
                }
                else{
                    app.globalData.all_in += temp;
                }
            }
            wx.setStorage({
                key: 'day_in',
                data: app.globalData.day_in
            })
            wx.setStorage({
                key: 'day_out',
                data: app.globalData.day_out
            })
            wx.setStorage({
                key: 'all_in',
                data: app.globalData.all_in
            })
            wx.setStorage({
                key: 'all_out',
                data: app.globalData.all_out
            })
            wx.setStorage({
                key: 'dataArray',
                data: app.globalData.dataArray
            })
        }
        wx.showToast({
          title: '保存成功',
          duration: 2000,
        })
        db.collection('test').doc('17e3426e61ce605601ac8f2304a84898').set({
            data:{
                day_in:app.globalData.day_in,
                day_out:app.globalData.day_out,
                all_in:app.globalData.all_in,
                all_out:app.globalData.all_out,
                dataArray:app.globalData.dataArray
              },
        })
    },
    clear:function(){
        this.setData({inputBeizhu:""});
        this.setData({inputPrice:""});
        this.setData({index:0});
    },
    bindPickerChange: function(e) {
        this.setData({
          index: e.detail.value
        })
    },
    Year: function(e) {
        this.setData({
          year: e.detail.value
        })
    },
    Month: function(e) {
        this.setData({
          month: e.detail.value
        })
    },
    Day: function(e) {
        this.setData({
          day: e.detail.value
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
        
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})